<template>
    <a-card :bordered="false">
        <a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form mb-4">
            <a-row :gutter="24">
                <a-col :span="6">
                    <a-form-item label="应用名称" name="searchKey">
                        <a-input v-model:value="searchFormState.searchKey" placeholder="请输入" />
                    </a-form-item>
                </a-col>
                <a-col :span="4">
                    <a-button type="primary" @click="table.refresh(true)">查询</a-button>
                    <a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
                </a-col>
            </a-row>
        </a-form>
        <s-table ref="table" :columns="columns" :data="loadData" :alert="options.alert.show" bordered
            :row-key="(record) => record.processInstanceId" :tool-config="toolConfig" :row-selection="options.rowSelection">
            <template #operator class="table-operator">
                <a-space>
                    <a-button danger @click="deleteRess()" :loading="uploading">删除</a-button>
                </a-space>
            </template>
            <template #bodyCell="{ column, record }">
                <template v-if="column.dataIndex === 'action'">
                    <a-space>
                        <a-popconfirm title="确定要删除吗？" @confirm="deleteRe(record)">
                            <a-button type="link" danger size="small">删除</a-button>
                        </a-popconfirm>
                    </a-space>
                </template>
            </template>
        </s-table>
    </a-card>
</template>

<script setup name="processStop">
import { message } from 'ant-design-vue'
import appinfoflowApi from '@/api/biz/appinfoflowApi'
let searchFormState = reactive({})
const searchFormRef = ref()
const table = ref()
const uploading = ref(false)
const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
const columns = [
    {
        title: '流程名称',
        dataIndex: 'wfMessagesAtt1'
    },
    {
        title: '流程ID',
        dataIndex: 'processInstanceId'
    },
    {
        title: '操作',
        dataIndex: 'action',
        align: 'center',
        width: '150px'
    }

]

let selectedRowKeys = ref([])
// 列表选择配置
const options = {
    alert: {
        show: false,
        clear: () => {
            selectedRowKeys = ref([])
        }
    },
    rowSelection: {
        onChange: (selectedRowKey, selectedRows) => {
            selectedRowKeys.value = selectedRowKey
        }
    }
}
const loadData = (parameter) => {
    const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
    return appinfoflowApi.getProcess(Object.assign(parameter, searchFormParam,{status:'1'})).then((data) => {
        // if (data?.records?.length > 0) {
        //     data.records.forEach((item, index) => {
        //         item.num = index + 1
        //     })
        // }
        return data
    }).catch(() => {
        return []
    })
}
// 删除
const deleteRe = (record) => {
    let params = {
        processInstanceids:[
            record.processInstanceId
        ]
    }
    appinfoflowApi.cancelProcess(params).then(() => {
        table.value.refresh(true)
    })
}
// 批量删除
const deleteRess = () => {
    if (selectedRowKeys.value.length < 1) {
        message.warning('请选择一条或多条数据')
        return false
    }
    const arr = selectedRowKeys.value.map((m) => {
        return m
    })
    let params = { processInstanceids:arr }
    appinfoflowApi.cancelProcess(params).then(() => {
        table.value.clearRefreshSelected()
    })
}
</script>
